package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.CommonUserResDTO;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.CommonUser;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.CertificationStatusDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements WorkerCertificationAuditService {

    @Resource
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;

    @Override
    public WorkerCertificationAudit findByServeProviderId(String serveProviderId) {
        return lambdaQuery().eq(WorkerCertificationAudit::getServeProviderId, serveProviderId)
                .one();
    }


    /**
     * 提交认证信息
     * @param workerCertificationUpdateDTO
     */
    @Override
    public void addAuthentication(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        WorkerCertificationAudit workerCertificationAudit = new WorkerCertificationAudit();
        BeanUtil.copyProperties(workerCertificationUpdateDTO, workerCertificationAudit);
        // createTime;
        workerCertificationAudit.setCreateTime(LocalDateTime.now());
        // updateTime;
        workerCertificationAudit.setUpdateTime(LocalDateTime.now());
        //  certificationStatus;
        workerCertificationAudit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
        //  certificationStatus;
        workerCertificationAudit.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        this.baseMapper.insert(workerCertificationAudit);
    }

    /**
     * 驳回原因
     * @return
     */
    @Override
    public WorkerCertificationAuditResDTO getReason() {
        WorkerCertificationAudit workerCertificationAudit =  this.baseMapper.selectById(UserContext.currentUserId());
        return BeanUtil.toBean(workerCertificationAudit, WorkerCertificationAuditResDTO.class);
    }

    /**
     * 获取认证状态
     * @param userType
     * @param providerId
     * @return
     */
    @Override
    public WorkerCertificationAuditResDTO getCertificationStatus(Integer userType, Long providerId){

//
//        if (ObjectUtil.equal(UserType.WORKER, userType)) {
//            WorkerCertificationAudit workerCertificationAudit = this.baseMapper.selectById(providerId);
//            System.out.println(workerCertificationAudit);
//            return BeanUtil.toBean(workerCertificationAudit, WorkerCertificationAuditResDTO.class);
//        }else {
//            AgencyCertification agencyCertification = agencyCertificationService.getById(providerId);
//            return BeanUtil.toBean(agencyCertification, CertificationStatusDTO.class);
//        }
        return null;
    }

    @Override
    public WorkerCertificationAudit getselectById(Long providerId) {
        return this.baseMapper.selectById(providerId);
    }

    @Override
    public PageResult<WorkerCertificationAuditResDTO> getpage(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {
        //1.分页查询用户
        Page<WorkerCertificationAudit> page = PageUtils.parsePageQuery(workerCertificationAuditPageQueryReqDTO, WorkerCertificationAudit.class);
        LambdaQueryWrapper<WorkerCertificationAudit> queryWrapper = Wrappers.<WorkerCertificationAudit>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(workerCertificationAuditPageQueryReqDTO.getName()), WorkerCertificationAudit::getName, workerCertificationAuditPageQueryReqDTO.getName())
                .eq(ObjectUtil.isNotEmpty(workerCertificationAuditPageQueryReqDTO.getAuditStatus()), WorkerCertificationAudit::getAuditStatus, workerCertificationAuditPageQueryReqDTO.getAuditStatus())
                .eq(ObjectUtil.isNotEmpty(workerCertificationAuditPageQueryReqDTO.getCertificationStatus()), WorkerCertificationAudit::getCertificationStatus, workerCertificationAuditPageQueryReqDTO.getCertificationStatus())
                .eq(ObjectUtil.isNotEmpty(workerCertificationAuditPageQueryReqDTO.getIdCardNo()), WorkerCertificationAudit::getIdCardNo, workerCertificationAuditPageQueryReqDTO.getIdCardNo());
        Page<WorkerCertificationAudit> workerCertificationAuditPage = workerCertificationAuditMapper.selectPage(page, queryWrapper);

        //3.封装数据
        return PageUtils.toPage(workerCertificationAuditPage, WorkerCertificationAuditResDTO.class);
    }

    /**
     * 审核
     */
    @Override
    public void audit(CertificationAuditReqDTO certificationAuditReqDTO) {
        WorkerCertificationAudit workerCertificationAudit = new WorkerCertificationAudit();
        BeanUtil.copyProperties(certificationAuditReqDTO, workerCertificationAudit);
        workerCertificationAudit.setAuditorId(UserContext.currentUserId());
        workerCertificationAudit.setAuditorName(UserContext.currentUser().getName());
        workerCertificationAudit.setAuditTime(LocalDateTime.now());
        workerCertificationAudit.setAuditStatus(CertificationStatusEnum.PROGRESSING.getStatus());
                this.baseMapper.updateById(workerCertificationAudit);
    }
}
